/**
 * 
 */
package org.t2.backstage.module.gameserver.service;

import org.banish.sql.core.util.QuerySet;
import org.gaming.service.advice.ResponseResult;
import org.gaming.service.service.redis.OneToManyService;
import org.springframework.stereotype.Service;
import org.t2.backstage.module.gameserver.model.GameServer;
import org.t2.backstage.util.T2HttpUtil;

import sh.t2.share.apiutil.GameApi;

/**
 * @author YY
 *
 */
@Service
public class GameServerService extends OneToManyService<GameServer> {

	public GameServer randomRunning() {
		for (GameServer gameServer : this.getEntityList(0)) {
			ResponseResult<String> serverResponse = T2HttpUtil.get(gameServer, GameApi.serverTestRunning);
			if (serverResponse == null) {
				continue;
			}
			logger.info("当前可用的服务为{}-{}-{}", gameServer.getId(), gameServer.getServerId(), gameServer.getName());
			return gameServer;
		}
		logger.info("没有可用的服务器");
		return null;
	}

	public GameServer getByPlatformAndServer(long platformId, int serverId) {
		QuerySet querySet = new QuerySet();
		querySet.addCondition("platform_id = ?", platformId);
		querySet.addCondition("server_id = ?", serverId);
		querySet.formWhere();
		return this.repository().getBaseDao().queryOneWhere(querySet.getWhere(), querySet.getParams());
	}

	@Override
	protected String namespace() {
		return "system";
	}
}
